package redsoxfan.gamejam.base;

import java.awt.Image;
import java.net.URL;

import javax.sound.sampled.AudioSystem;
import javax.swing.ImageIcon;

/**
 * Load game resources
 * @author RedSoxFan
 * @since 20 Apr 14
 * @version 1.0
 */
public class GameResource {
	/**
	 * Hide constructor
	 */
	private GameResource() {
	}
	/**
	 * Load an image as a Pixtact object
	 * @param path - Path to image
	 * @return Image as a Pixtact object
	 */
	public static Pixtact loadAsPixtact(String path) {
		try {
			return Pixtact.createFromImage(loadImage(path));
		} catch (Exception e) {
		}
		return null;
	}
	/**
	 * Load an image as a Pixtact object
	 * @param path - Path to image
	 * @param width - Width to resize the image to
	 * @param height - Height to resize the image to
	 * @return Image as a Pixtact object
	 */
	public static Pixtact loadAsPixtact(String path, int width, int height) {
		try {
			Pixtact p = Pixtact.createFromImage(loadImage(path));
			p.resize(width, height);
			return p;
		} catch (Exception e) {
		}
		return null;
	}
	/**
	 * Load audio as an Audio object
	 * @param path - Path to the audio file
	 * @param music - Is the background music or a sound effect?
	 * @return Audio object for the audio file
	 */
	public static Audio loadAudio(String path, boolean music) {
		try {
			return new Audio(AudioSystem.getAudioInputStream(loadResource(path)), music);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * Load image
	 * @param path - Path to image
	 * @return Image
	 */
	public static Image loadImage(String path) {
		try {
			return new ImageIcon(loadResource(path)).getImage();
		} catch (Exception e) {
		}
		return null;
	}
	/**
	 * Load image
	 * @param path - Path to image
	 * @param width - Width to resize image to
	 * @param height - Height to resize image to
	 * @return Image
	 */
	public static Image loadImage(String path, int width, int height) {
		try {
			return loadAsPixtact(path, width, height).getImage();
		} catch (Exception e) {
		}
		return null;
	}
	/**
	 * Load audio. This Audio file will be constructed as background music.
	 * @param path - Path to audio file
	 * @return Audio object
	 */
	public static Audio loadMusic(String path) {
		return loadAudio(path, true);
	}
	/**
	 * Get the URL to a resource
	 * @param path - Path to the resource
	 * @return URL for the resource
	 */
	public static URL loadResource(String path) {
		try {
			return GameResource.class.getResource(path);
		} catch (Exception e) {
		}
		return null;
	}
	/**
	 * Load audio. This Audio file will be constructed as sound effects.
	 * @param path - Path to audio file
	 * @return Audio object
	 */
	public static Audio loadSound(String path) {
		return loadAudio(path, false);
	}
}
